package com.sc.ktapplication.common.util

import android.content.Context
import android.content.pm.PackageManager

object PackageUtils {
    fun getPackageName(context: Context): String {
        return context.packageName
    }

    fun getVersionName(context: Context): String? {
        val pm = context.packageManager
        try {
            val packageInfo =
                pm.getPackageInfo(context.packageName, PackageManager.GET_ACTIVITIES)
            if (packageInfo != null) {
                return if (packageInfo.versionName == null) "null" else packageInfo.versionName
            }
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return null
    }

    fun getVersionCode(context: Context): Int {
        val pm = context.packageManager
        try {
            val packageInfo =
                pm.getPackageInfo(context.packageName, PackageManager.GET_ACTIVITIES)
            if (packageInfo != null) {
                return packageInfo.versionCode
            }
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return 0
    }
}